package io.bdmc.modules.job.task;

import io.bdmc.modules.bss.entity.SmkDevice;
import io.bdmc.modules.bss.service.ISmkDeviceService;
import io.bdmc.modules.bss.service.ISmkHeartbeatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

/**
 * 定时数据库任务
 * 
 * testTask为spring bean的名称
 * 
 * @author huang lu
 * @email hlkyo@qq.com
 * @date 2016年11月30日 下午1:34:24
 */
@Component("MysqlDataTask")
public class MysqlDataTask {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    ISmkHeartbeatService _heartSvs;

    @Resource
    ISmkDeviceService _devSvs;

    public <SysUser> void heartbeatClear(String params) {
        logger.info("我是带参数的heartbeatClear方法，定时清理历史心跳数据，正在被执行，参数为：" + params);
        Integer count = _heartSvs.deleteHeartbeat();
        logger.info("删除心跳数据条数："+count);
    }

    public void offlineJudge() {
        logger.info("我是offlineJudge方法，正在被执行，无参数，定时判定离线的设备");
        List<SmkDevice> offlineList = _devSvs.listHKOfflineDevice();
        if(offlineList.size()>0){
            LocalDateTime now = LocalDateTime.now();
            for (SmkDevice dev : offlineList) {
                dev.setDevStatus(2);
                dev.setUpdateDate(now);
            }
            _devSvs.updateBatchById(offlineList);
            logger.info("已更新海康设备状态的数据条数："+offlineList.size());
            return;
        }
        logger.info("全部设备已是最新状态，无需更新");
    }
}